<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card
        v-loading="loading"
        class="tree-card"
      >
        <!-- 头部布局 -->
        <TreeTools
          :node-data="company"
          :is-root="true"
          @add-List="addList"
        />
        <!-- 树形组件
        data: 树形组件数据
        props : 指定树形组件的文件或后代键名
        default-expand-all ： 默认展开
        -->
        <el-tree :data="departs" :props="defaultProps" default-expand-all>
          <template v-slot="{data}">
            <TreeTools
              :node-data="data"
              @del-List="getDepartments"
              @add-List="addList"
              @adit-List="aditList"
            />
          </template>
        </el-tree>
      </el-card>
    </div>
    <!-- @closeDialog="closeDialog" -->
    <AddList
      ref="addList"
      :datalist="nodeData"
      :dialog-visible.sync="dialogVisible"
      :list="list"
      @addDepots="getDepartments"
    /></div>
</template>

<script>
import AddList from './components/add-List.vue'
import { transListToTree } from '@/utils/index'
import { reqGetDepartments } from '@/api/departments'
import TreeTools from './components/tree-tools.vue'
export default {
  name: 'Departments',
  components: {
    TreeTools,
    AddList
  },
  data() {
    return {
      loading: true,
      dialogVisible: false,
      departs: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      company: {},
      nodeData: {},
      list: []
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      this.loading = true
      const { data: { depts, companyName }} = await reqGetDepartments()
      this.departs = transListToTree(depts, '')
      this.list = depts
      this.company = { name: companyName, manager: '负责人', id: '' }
      this.loading = false
    },
    // 关闭dialog的函数
    closeDialog() {
      this.dialogVisible = false
    },
    // 添加的addList
    addList(data) {
      this.dialogVisible = true
      this.nodeData = data
    },
    // 编辑的editList
    aditList(data) {
      this.dialogVisible = true
      this.nodeData = data
      this.$nextTick(() => {
        this.$refs.addList.getDepartDetail()
      })
    }
  }
}
</script>

<style scoped lang ="scss">
.tree-card {
  padding: 30px 30px;
  font-size:14px;
}
.el-tree {
  ::v-deep {
    // 小三角的样式, 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开 +
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开 -
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before{
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before  {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}

</style>
